package cn.gzsxt.service.impl;

import java.util.Collections;
import java.util.List;

import cn.gzsxt.dao.SysUserDao;
import cn.gzsxt.dao.impl.SysUserDaoImpl;
import cn.gzsxt.po.PageResult;
import cn.gzsxt.po.SysUser;
import cn.gzsxt.query.SysUserQueryObject;
import cn.gzsxt.service.SysUserService;

public class SysUserServiceImpl implements SysUserService {

	private SysUserDao dao = new SysUserDaoImpl();

	@Override
	public SysUser login(String uname, String pwd) {
		return dao.findByUnameAndPwd(uname, pwd);
	}

	@Override
	public SysUser checkUname(String uname) {
		return dao.findByUname(uname);
	}

	@Override
	public int save(SysUser sysUser) {
		return dao.insert(sysUser);
	}

	@Override
	public PageResult list(SysUserQueryObject qo) {
		//1.查询用户总数
		Integer totalCount = dao.findTotalCountByCondition(qo);
		
		//创建PageResult对象,用于封装列表页面的所有数据(包括分页相关信息)
		PageResult pageResult = null;
		//如果查询的总数大于0条就去查询数据列表,如果等于0就没必要查询数据
		if(totalCount != 0){
			//2.根据条件查询数据列表
			List<SysUser> users = dao.findByCondition(qo);
			pageResult = new PageResult(users, totalCount, qo.getPageSize(), qo.getCurrentPage());
		}else{
			pageResult = new PageResult(Collections.EMPTY_LIST, 0, qo.getPageSize(), qo.getCurrentPage());
		}
		return pageResult;
	}

	@Override
	public int deleteUser(Integer id) {
		return dao.deleteById(id);
	}

	@Override
	public int updateSysUser(SysUser sysUser) {
		return dao.updateSysUser(sysUser);
	}

}
